package com.liveneo.system.service;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.entity.PrpmUser;
import com.liveneo.adapter.base.entity.PrpmUserExample;
import com.liveneo.adapter.base.mapper.PrpmUserMapper;
import com.liveneo.adapter.base.support.BaseService;

/**
 * 用户信息服务类
 */
@Service
public class UserService extends BaseService<PrpmUserMapper, PrpmUser> {
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);

    /**
     * 新增用户
     * @param entity
     * @return
     */
    public boolean savePrpmUser(PrpmUser entity) {
        try {
            int res = this.mapper.insert(entity);
            return res > 0;
        } catch (Exception e) {
            LOG.info("save用户信息错误---->：" + e.getMessage());
        }
        return false;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    public boolean delPrpmUser(List<String> ids) {
        try {
            int res = 0;
            for (String id : ids) {
                // 查询用户信息
                res = this.mapper.deleteByPrimaryKey(id);
                LOG.info("删除用户编号为---->：" + id + "删除状态---->：" + res);
            }
            return res > 0;
        } catch (Exception e) {
            LOG.info("del用户信息错误---->：" + e.getMessage());
        }
        return false;
    }

    /**
     * 更新用户信息
     * @param entity
     * @return
     */
    public boolean updatePrpmUser(PrpmUser entity) {
        try {
            // 修改用户信息
            int res = this.mapper.updateByPrimaryKey(entity);
            return res > 0;
        } catch (Exception e) {
            LOG.info("修改用户信息错误---->：" + e.getMessage());
        }
        return false;
    }

    /**
     * 根据组织id查询用户信息
     * @param orgId 可传空
     * @return
     */
    public List<PrpmUser> queryUserAll(String orgId) {
        PrpmUserExample example = new PrpmUserExample();
        example.createCriteria().andOrgIDEqualTo(orgId);
        List<PrpmUser> userList = this.mapper.selectByExample(example);
        return userList;
    }

    public PrpmUser findByUsername(String username) {
        PrpmUserExample example = new PrpmUserExample();
        example.createCriteria().andUserNameEqualTo(username);
        List<PrpmUser> userList = this.mapper.selectByExample(example);
        return (userList != null && userList.size() > 0) ? userList.get(0) : null;
    }

    public PrpmUser findByPhoneNo(String phoneNo) {
        PrpmUserExample example = new PrpmUserExample();
        example.createCriteria().andMobilePhoneEqualTo(phoneNo);
        List<PrpmUser> userList = this.mapper.selectByExample(example);
        return (userList != null && userList.size() > 0) ? userList.get(0) : null;
    }

    public PrpmUser findByUserCode(String userCode) {
        PrpmUserExample example = new PrpmUserExample();
        example.createCriteria().andUserCodeEqualTo(userCode);
        List<PrpmUser> userList = this.mapper.selectByExample(example);
        return (userList != null && userList.size() > 0) ? userList.get(0) : null;
    }
}
